var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var users = require('../models/users');
var bodyParser = require('body-parser');
var app = express();

/* GET users listing. */

mongoose.connect('mongodb://127.0.0.1:27017/myBlog');

mongoose.connection.on('connected',function () {
    console.log("MongoDB connected success.")
});
mongoose.connection.on('err',function () {
    console.log("MongoDB connected fail.")
});
mongoose.connection.on('disconnected',function () {
    console.log("MongoDB connected disconnected.")
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
router.get('/',function (req, res, next) {
    res.send("ok")
})
router.post('/signIn', function(req, res, next) {
    users.find({name:req.body.name,password: req.body.password},function (err,doc) {
      if(err){
        res.json({
            status: '1',
            msg: err.message
        })
      }else{
        res.json({
            status: '0',
            msg: '',
            result: {
              count: doc.length,
              list: doc
            }
        });

      }
    })
});
router.post('/signUpCheck',function(req, res, next) {
    users.find({name: req.body.account},function (err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});
router.post('/signUp',function (req, res, next) {
    console.log(req.body.Account);
    console.log(req.body.Password);
    users.create({'name': req.body.Account,'password': req.body.Password,'role': 1},function(err,doc){
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});



module.exports = router;
